package cn.threewaters.qtyy.service.sys;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.google.common.collect.Lists;

import cn.threewaters.qtyy.dit.one.dao.SysUserDao;
import cn.threewaters.qtyy.dit.one.entity.SysUser;
import cn.threewaters.qtyy.dit.one.entity.SysUserRole;
import cn.threewaters.qtyy.dit.one.entity.base.OneBaseModel;

@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserDao, SysUser> implements SysUserService {

	@Autowired
	private SysUserRoleService sysUserRoleService;

	@Override
	@Transactional
	public SysUser insertSysUser(SysUser sysUser) {
		this.insert(sysUser);
		// String userId = sysUser.getId();
		//
		// List<String> roles = sysUser.getRoles();
		// List<SysUserRole> sysUserRoles = Lists.newArrayList();
		// for (String roleId : roles) {
		// SysUserRole sysUserRole = new SysUserRole();
		// sysUserRole.setRoleId(roleId);
		// sysUserRole.setUserId(userId);
		// sysUserRoles.add(sysUserRole);
		// }
		// if (sysUserRoles.size() > 0) {
		// sysUserRoleService.insertBatch(sysUserRoles);
		// }
		return sysUser;
	}

	@Override
	@Transactional
	public SysUser updateSysUser(SysUser sysUser) {
		this.updateById(sysUser);
		String userId = sysUser.getId();
		List<SysUserRole> oldSysUserRoleList = sysUserRoleService.selectList(
				new EntityWrapper<SysUserRole>().eq("active_flag", OneBaseModel.ACTIVE_FLAG_YES).eq("user_id", userId));
		List<String> oldSysUserRoleIdList = Lists.newArrayList();
		for (SysUserRole sysUserRole : oldSysUserRoleList) {
			oldSysUserRoleIdList.add(sysUserRole.getRoleId());
		}
		List<String> newSysUserRoleIdList = sysUser.getRoles() == null ? Lists.newArrayList() : sysUser.getRoles();

		// 需要新增的
		List<String> waitAddList = Lists.newArrayList();
		waitAddList.addAll(newSysUserRoleIdList);
		waitAddList.removeAll(oldSysUserRoleIdList);
		List<SysUserRole> sysUserRoleList = Lists.newArrayList();
		for (String roleId : waitAddList) {
			SysUserRole sysUserRole = new SysUserRole();
			sysUserRole.setUserId(userId);
			sysUserRole.setRoleId(roleId);
			sysUserRoleList.add(sysUserRole);
		}
		if (sysUserRoleList.size() > 0) {
			sysUserRoleService.insertBatch(sysUserRoleList);
		}
		// 需要删除的
		List<String> waitDeleteList = Lists.newArrayList();
		waitDeleteList.addAll(oldSysUserRoleIdList);
		waitDeleteList.removeAll(newSysUserRoleIdList);
		if (waitDeleteList.size() > 0) {
			sysUserRoleService
					.delete(new EntityWrapper<SysUserRole>().eq("user_id", userId).in("role_id", waitDeleteList));
		}

		return sysUser;
	}

	@Override
	public boolean isExitLoginName(String loginName) {

		int result = this.selectCount(new EntityWrapper<SysUser>().eq("login_name", loginName));
		if (result == 0) {
			return true;
		}
		return false;
	}

}
